package com.jlz.managers.def;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.jlz.beans.def.ValuetypeBean;
import com.julewa.db.DataType;
import com.julewa.db.Store;

@Service
public class Valuetypes {
	private Store<ValuetypeBean> store = 
		new Store<ValuetypeBean>(ValuetypeBean.class);
	public static final int TYPE_ID_NODE = -1;
	public static final ValuetypeBean NODE = new ValuetypeBean();
	static{
		NODE.setID(TYPE_ID_NODE);
	}
	//
	@SuppressWarnings("unused")
	@PostConstruct
	private void init(){
		//创建初始数据类型
		List<ValuetypeBean> types = store.select("all", null);
		if(!CollectionUtils.isEmpty(types))return;
		//
		ValuetypeBean vt = store.create();
		vt.setLabel("数值");
		vt.setCode("Number");
		vt.setParsable(false);
		vt.setType(DataType.INT.getValue());
		this.insert(vt);
		//
		vt = store.create();
		vt.setLabel("短语");
		vt.setCode("Label");
		vt.setParsable(true);
		vt.setMax(128);
		vt.setType(DataType.STRING.getValue());
		this.insert(vt);
	};
	
	private static final List<DataType> DataTypes = 
		Collections.unmodifiableList(Arrays.asList(DataType.values()));
	public List<DataType> getDataTypes(){
		return DataTypes;
	}
	
	
	//
	public ValuetypeBean get(int valuetypeId) {
		return store.get(valuetypeId);
	}
	
	public ValuetypeBean create(){
		return store.create();
	}
	
	public boolean insert(ValuetypeBean bean){
		bean.setID(store.generateID().intValue());
		return store.insert(bean);
	}
	
	public boolean update(ValuetypeBean bean){
		return store.update(bean);
	}

	public boolean delete(ValuetypeBean bean){
		return store.delete(bean);
	}
	
	public List<ValuetypeBean> getAll(){
		return store.select("all", null);
	}
	
}
